package com.xcw.jac.deoc;

import lombok.SneakyThrows;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class DataProcessingTask implements Runnable {
        private HSSFRow row;
        private HSSFSheet sheet;
        private Object o;
        private Field[] fields;



    public DataProcessingTask(HSSFRow rows, Object o, Field[] fields, HSSFSheet sheet) {
        this.row = rows;
        this.o = o;
        this.fields = fields;
        this.sheet = sheet;
    }

    @SneakyThrows
    @Override
        public void run() {
            // 处理数据的逻辑
        System.out.println(o.getClass());
        System.out.println(o);
        for (Field f : fields) {
            MyExcel annotation = f.getAnnotation(MyExcel.class);
            String fieldName = f.getName();
            System.out.println(fieldName);
            String getterMethodName = "get" + capitalize(fieldName);
            Method getterMethod = o.getClass().getMethod(getterMethodName);
            Object value = getterMethod.invoke(o);
            HSSFCell cell = row.createCell(annotation.index());
            cell.setCellValue(String.valueOf(value));
            System.out.println(annotation.index());
            System.out.println(annotation.value());
            sheet.autoSizeColumn(annotation.index(),true);
        }

        }
    private static String capitalize(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }

        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
}